package cn.jit.ssm.exception;

import cn.jit.ssm.intercept.SecureValidInterceptor;
import cn.jit.ssm.support.JSONReturn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 处理controller层的异常
 * 不以500状态对请求进行响应
 * @author: Cecurio
 * @create: 2017-12-07 15:51
 **/
@ControllerAdvice
public class ExceptionIntercept implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(ExceptionIntercept.class);

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({Exception.class})
    public JSONReturn exception(Exception ex, HttpServletRequest request, HttpServletResponse response) {
        // 打印堆栈错误信息
        ex.printStackTrace();
        // 记录错误信息
        logger.warn(ex.getMessage(),ex);
        return JSONReturn.buildFailure("服务器错误！");
    }
}
